﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//      We are given integer number n, value v (v=0 or 1) and a position p. 
//      Write a sequence of operators that modifies n to hold the value v at the position p from the binary representation of n.
//      Example: n = 5 (00000101), p=3, v=1  13 (00001101)
//      n = 5 (00000101), p=2, v=0  1 (00000001)

namespace _12ModifyGivenBit
{
    class _12ModifyGivenBit
    {
        static void Main(string[] args)
        {
            Console.Write("Enter an integer:");
            int n = int.Parse(Console.ReadLine());
            Console.Write("Enter position of bit:");
            int p = int.Parse(Console.ReadLine());
            Console.Write("Enter value of bit (only 0 or 1):");
            int v = int.Parse(Console.ReadLine());            

            Console.WriteLine("Before: {0}", Convert.ToString(n, 2).PadLeft(32, '0'));
            if (v == 0)
            {
                n = n & (~(1 << p));
            }
            else
            {
                n = n | (1 << p);
            }
            Console.WriteLine("After:  {0}", Convert.ToString(n, 2).PadLeft(32, '0'));
        }
    }
}
